﻿using StableDiffusionTools.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StableDiffusionTools.Controls
{
    //TODO: add Wrap property
    public class NumericUpDownEx : NumericUpDown
    {
        public bool AllowRollover { get; set; } = false;
        public decimal LargeIncrement { get; set; } = 10;
        public NumericUpDownEx() : base() { }

        //Overrides
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            ((HandledMouseEventArgs)e).Handled = true;
            int deltaTimes = Math.Abs(e.Delta) / 120;
            decimal amount = ModifierKeys.HasFlag(Keys.Control) ? Increment : LargeIncrement;
            decimal newValue = Value + ((e.Delta > 0) ? Increment * deltaTimes : -Increment * deltaTimes);
            if (AllowRollover)
            {
                if (newValue > Maximum)
                {
                    Value = Minimum;
                }
                else if (newValue < Minimum)
                {
                    Value = Maximum;
                }
                else
                {
                    Value = newValue;
                }
            }
            else
            {
                Value = MathUtil.Clamp(newValue, Minimum, Maximum);
            }

            base.OnMouseWheel(e);
        }

        public override void UpButton()
        {
            if (UserEdit)
            {
                ParseEditText();
            }

            decimal newValue = Value;

            // Operations on Decimals can throw OverflowException.
            try
            {
                newValue += this.Increment;

                if (newValue > Maximum)
                {
                    if (AllowRollover)
                    {
                        newValue = Minimum;
                    }
                    else
                    {
                        newValue = Maximum;
                    }
                }
            }
            catch (OverflowException)
            {
                newValue = Maximum;
            }

            Value = newValue;
            //base.UpButton();
        }

        public override void DownButton()
        {
            if (UserEdit)
            {
                ParseEditText();
            }

            decimal newValue = Value;

            // Operations on Decimals can throw OverflowException.
            try
            {
                newValue -= this.Increment;

                if (newValue < Minimum)
                {
                    if (AllowRollover)
                    {
                        newValue = Maximum;
                    }
                    else
                    {
                        newValue = Minimum;
                    }
                }
            }
            catch (OverflowException)
            {
                newValue = Minimum;
            }

            Value = newValue;
            //base.DownButton();
        }
    }
}